
import 'dart:convert';
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/pojo/Enums.dart';
import 'package:im_app/im/service/SynDataService.dart';
import 'package:im_app/im/service/LoginService.dart';
import 'package:im_app/im/socket/service/RpcPullDataService.dart';
import 'package:im_app/im/socket/ImWebSocketIsolate.dart';
import 'package:im_app/im/socket/dto/business/Login.dart';
import 'package:im_app/im/socket/dto/business/PullData.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/ImSingleton.dart';
import 'package:logger/logger.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

Future<void> main() async {

  test("syn common data", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    ImWebSocketIsolate imWebSocket = new ImWebSocketIsolate('ws://127.0.0.1:9290/imWebsocket');
    await imWebSocket.start();
    ImSingleton.getInstance()?.imWebSocket = imWebSocket;

    var logger = Logger();
    LoginReq loginReq = LoginReq(userId: "test00100", password: "123456", machineCode: "demo",
        tenantCode: "0000", terminalType: "android");
    logger.i("login json: " + loginReq.toJson().toString());
    LoginService loginService = LoginService();
    LoginResp? loginResp = await loginService.login(loginReq, LoginType.password);
    logger.i("login success result : " + jsonEncode(loginResp));

    SynDataService synDataService = SynDataService();
    // await synDataService.synCommonData("test00100");
    var duration = const Duration(seconds: 10);
    logger.i('Start sleeping');
    sleep(duration);
    imWebSocket.close();
    database.close();
  }, timeout: Timeout.factor(68));

  test("pull sessions", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");

    ImWebSocketIsolate imWebSocket = new ImWebSocketIsolate('ws://127.0.0.1:9290/imWebsocket');
    await imWebSocket.start();
    ImSingleton.getInstance()?.imWebSocket = imWebSocket;

    LoginReq loginReq = LoginReq(userId: "test00600", password: "123456", machineCode: "demo",
        tenantCode: "0000", terminalType: "android");
    LoginService loginService = LoginService();
    LoginResp? loginResp = await loginService.login(loginReq, LoginType.password);

    int pageSize = 100;
    var sessionPullReq = SessionPullReq(pageSize);
    sessionPullReq.startDate = null;

    var pullDataService = new RpcPullDataService();
    var sessionItemList = await pullDataService.pullSessions(sessionPullReq);
    for (var sessionItem in sessionItemList.sessionItemList) {
      print("mg ${sessionItem.groupId}, ${sessionItem.userId}");
    }
    database.close();
  });

  test("pullMessageList", () async {
    sqfliteFfiInit();
    databaseFactory = databaseFactoryFfi;
    Database database = await DbUtil.connect("imtest.db");
    ImSingleton.getInstance()?.dataBase = database;

    ImWebSocketIsolate imWebSocket = new ImWebSocketIsolate('ws://127.0.0.1:9290/imWebsocket');
    await imWebSocket.start();
    ImSingleton.getInstance()?.imWebSocket = imWebSocket;

    LoginReq loginReq = LoginReq(userId: "test00100", password: "123456", machineCode: "demo",
        tenantCode: "0000", terminalType: "android");
    LoginService loginService = LoginService();
    LoginResp? loginResp = await loginService.login(loginReq, LoginType.password);

    int pageSize = 500;
    Set<String> set = {};
    set.add("test00101");
    // var messagePullP2psReq = MessagePullBySessionIdsReq(terminalType: TerminalType.android, pageSize: pageSize, sessionIds: set);
    // PullDataService pullDataService = new PullDataService();
    // MessageListItem? messageListItem = await pullDataService.pullMessageBySessionIds(messagePullP2psReq);
    // for (MessageObject mg in messageListItem.messageObjectList) {
    //   print("mg ${mg.groupCode}, ${mg.toUserId}");
    // }
    imWebSocket.close();
    database.close();
  });

}
